package com.dtruth.dataflow.dao;

import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.dtruth.dataflow.dao.BaseDAO;
import com.dtruth.dataflow.model.Fe_news_fingerprint;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class NewsFingerPrintDAO extends BaseDAO<Fe_news_fingerprint> {

	/**
	 * 
	 * @return
	 */
	public Multimap<String, BigInteger> mapAllData() throws SQLException {
		Multimap<String, BigInteger> multiMap = ArrayListMultimap.create();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select news_key,title,content from fe_news_fingerprint";
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			String newsKey = rs.getString("news_key");
			BigInteger title = new BigInteger(rs.getString("title"));
			BigInteger content = new BigInteger(rs.getString("content"));
			multiMap.put(newsKey, title);
			multiMap.put(newsKey, content);
		}
		close(pstmt, rs);
		return multiMap;
	}

	public boolean insertAndUpdate(String news_key, BigInteger bi_title,
			BigInteger bi_content) throws SQLException {
		PreparedStatement pstmt = null;
		String sql = "insert into fe_news_fingerprint(news_key,title,content) values(?,?,?) ON DUPLICATE KEY UPDATE rcount=rcount+1";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, news_key);
		pstmt.setString(2, bi_title.toString());
		pstmt.setString(3, bi_content.toString());
		if (pstmt.execute()) {
			close(pstmt, null);
			return true;
		}
		return false;
	}

	public void rcountAddup(String news_key) throws SQLException {
		PreparedStatement pstmt = null;
		String sql = "update fe_news_fingerprint set rcount=rcount+1 where news_key=?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, news_key);
		if (pstmt.execute()) {
			close(pstmt, null);
		}
	}

	public int update(String news_key, BigInteger bi_title,
			BigInteger bi_content) throws SQLException {
		PreparedStatement pstmt = null;
		String sql = "update fe_news_fingerprint set title=?,content=? where news_key=?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, bi_title.toString());
		pstmt.setString(2, bi_content.toString());
		pstmt.setString(3, news_key);
		int result = pstmt.executeUpdate();
		return result;
	}

	/**
	 * 
	 * @param news_key
	 * @return
	 * @throws SQLException
	 */
	public long getTimeByNewsKey(String news_key) throws SQLException {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		long createtime = 0;
		String sql = "select create_time from fe_news_fingerprint where news_key=?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, news_key);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			createtime = rs.getTimestamp("create_time").getTime();
		}
		close(pstmt, rs);
		return createtime;
	}

	public Map<String, String> findNewsKey(String colName, String value)
			throws SQLException {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Map<String, String> map = new HashMap<String, String>();
		String sql = "select news_key,content from fe_news_fingerprint where "
				+ colName + "=?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, value);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			map.put(rs.getString("news_key"), rs.getString("content"));
		}
		close(pstmt, rs);
		return map;
	}

}
